package com.ice.work.moment.crawler.service.impl;

import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.constant.CommonSystemConstant;
import com.ice.work.moment.crawler.dao.trading.TradingRepository;
import com.ice.work.moment.crawler.service.ITradingService;
import com.ice.work.moment.facade.crawler.model.Trading;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Objects;

/**
 * @Description 股票交易信息
 * @Author DELL
 * @Create 2019-05-05
 * @Since 1.0.0
 */
@Service
@Slf4j
public class TradingServiceImpl implements ITradingService {

    @Autowired
    private TradingRepository tradingRepository;

    @Override
    public Boolean save(Collection<Trading> tradings) throws BaseBizException {
        Assert.notEmpty(tradings, "tradings not null");
        return tradingRepository.saveBatch(tradings, CommonSystemConstant.BATCH_SIZE);
    }

    @Override
    public Boolean save(Trading trading) throws BaseBizException {
        Assert.notNull(trading, "trading not null");
        return tradingRepository.save(trading);
    }

    @Override
    public Boolean saveOrUpdate(Trading trading) throws BaseBizException {
        Assert.notNull(trading, "trading not null");
        Trading exists = tradingRepository.findByShareIdAndTradingAtAndDataStatus(trading);
        if (Objects.nonNull(exists)) {
            trading.setId(exists.getId());
        }
        return tradingRepository.saveOrUpdate(trading);
    }
}
